package bg.cinema_system.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import bg.cinema_system.global.ConnectionJDBC;
import bg.cinema_system.pojo.Mail;

public class MailDao {

	public static Mail getMailByType(String mailType) throws SQLException {

		Connection dbConnection = ConnectionJDBC.getConnection();

		if (dbConnection == null) {
			throw new SQLException("Class HallDAO: Can't get database connection");
		}

		String sqlStatement = "SELECT mail_id, subject, body, mail_type FROM mails WHERE mail_type = ?";

		PreparedStatement ps = null;
		ResultSet rs = null;
		Mail mail = null;
		try {
			ps = dbConnection.prepareStatement(sqlStatement);
			ps.setString(1, mailType);
			
			// Get customer data from database
			rs = ps.executeQuery();
			if (rs.next()) {
				mail = new Mail(rs);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SQLException(e);
		} finally {
			try {
				ps.close();
				rs.close();
				dbConnection.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new SQLException(e);
			}
		}
		return mail;
	}

}
